package rabbitmq.consume;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
//使用queuesToDeclare属性,如果不存在则会创建队列

public class RabbitDemoConsumer {

    /**
     * 自动确认ack
     *
     * @author 千里明月
     * @date 2023/10/15
     **/
//    @RabbitListener(queues = "${rabbitmq.direct.queue:directQueue}")
    public void process(@Payload Map<String, Object> map) throws IOException {
        System.out.println("队列test_queue收到消息：" + map.toString());
    }

    //自动创建队列，Exchange 与 Queue绑定
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "${rabbitmq.direct.queue:directQueue}", durable = "true", autoDelete = "false", exclusive = "false"),
            exchange = @Exchange(value = "directExchange"),
            key = "direct"
    ))
    public void initDirect(@Payload Map<String, Object> map) {
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topicQueue", durable = "true", autoDelete = "false", exclusive = "false"),
            exchange = @Exchange(value = "topicExchange", type = ExchangeTypes.TOPIC),
            key = "topic"
    ))
    public void initTopic(@Payload Map<String, Object> map) {
    }
}
